﻿using Cinemachine;
using Services;
using Services.Input;
using Services.StaticData;
using UnityEngine;

namespace UI.Support
{
    public class Zoom : MonoBehaviour
    {
        [SerializeField] private CinemachineVirtualCamera _camera;
        private IStaticDataService _staticDataService;
        private IInputService _inputService;
        private PlayerControls _controls;
        private GameStaticData _gameData;
        private float _minCameraSize;
        private float _maxCameraSize;
        private float _zoomStep;

        private void Start()
        {
            _staticDataService = AllServices.Container.Single<IStaticDataService>();
            _inputService = AllServices.Container.Single<IInputService>();
            _gameData = _staticDataService.Game;
            _controls = _inputService.GetControls();
            _controls.Enable();
            InitCamera();
        }

        private void Update()
        {
            var zoom = _controls.UI.Zoom.ReadValue<float>();
            CheckZoom(zoom);
        }

        private void InitCamera()
        {
            _minCameraSize = _gameData.MinCameraSize;
            _maxCameraSize = _gameData.MaxCameraSize;
            _zoomStep = _gameData.ZoomStep;
        }

        private void CheckZoom(float zoom)
        {
            switch (zoom)
            {
                case > 0:
                    ZoomIn();
                    break;
                case < 0:
                    ZoomOut();
                    break;
            }
        }

        private void ZoomIn()
        {
            if (_camera.m_Lens.OrthographicSize > _minCameraSize)
                _camera.m_Lens.OrthographicSize -= _zoomStep;
        }

        private void ZoomOut()
        {
            if (_camera.m_Lens.OrthographicSize < _maxCameraSize)
                _camera.m_Lens.OrthographicSize += _zoomStep;
        }
    }
}